package com.graduation.service;

import com.graduation.mapper.UserMapper;
import com.graduation.model.UserEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description
 * @Author YaoQi
 * @Date 2018/3/6 23:51
 */
@Service
public class UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserService.class);
    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisService redisService;

    /**
     * 获取所有userEntity
     *
     * @return
     */
    public List<UserEntity> getUserList() {

        if (redisService.exists("userList")) {
            List<UserEntity> userEntityList = (List<UserEntity>) redisService.get("userList");
            logger.info("getFromRedis, result is " + userEntityList);
            return userEntityList;
        } else {
            List<UserEntity> userEntityList = userMapper.getUserList();
            if (redisService.set("userList", userEntityList)) {
                logger.info("userList is set to Redis");
            }
            return userEntityList;
        }
    }
}
